package com.bilibili.bilibili.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.activity.Bangindex;
import com.bilibili.bilibili.adapter.Bangcardadapter;
import com.bilibili.bilibili.adapter.Bangumimothadapter;
import com.bilibili.bilibili.adapter.Bangumirecycleadapter;
import com.bilibili.bilibili.bean.BangcardBean;
import com.bilibili.bilibili.bean.BangumiBean;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GlideImageLoader;
import com.bilibili.bilibili.utils.GsonUtil;
import com.bilibili.bilibili.utils.URLUtils;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 番剧界面
 * 张贤伟
 */
public class BangumiFragment extends Fragment {


    private View view;
    private Banner banner;
    private RecyclerView recyclerView;
    private Bangumirecycleadapter bangumirecycleadapter;
    private RecyclerView bangumi_monthrecycle;
    private Bangumimothadapter bangumimothadapter;
    private RecyclerView cardrecycle;
    private Bangcardadapter bangcardadapter;
    private SwipeRefreshLayout swipeRefreshLayout;

    public BangumiFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_bangumi, container, false);
        initid();
        return view;

    }

    private void initid() {
        //索引
        TextView layout_bangumi_index = (TextView) view.findViewById(R.id.layout_bangumi_index);
        layout_bangumi_index.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Bangindex.class);
                startActivity(intent);
            }
        });

        //放松表
        TextView layout_bangumi_schedule = (TextView) view.findViewById(R.id.layout_bangumi_schedule);
        layout_bangumi_schedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(getActivity(), Bangindex.class);
                startActivity(intent1);
            }
        });
        //追番
        TextView layout_chase_bangumi = (TextView) view.findViewById(R.id.layout_chase_bangumi);
        layout_chase_bangumi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(getActivity(), Bangindex.class);
                startActivity(intent2);
            }
        });

        banner = (Banner) view.findViewById(R.id.bangumi_banner);
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        recyclerView = (RecyclerView) view.findViewById(R.id.bangumi_recycle);
        bangumi_monthrecycle = (RecyclerView) view.findViewById(R.id.bangumi_monthrecycle);
        //停止刷新
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipebang);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        //番剧cardview的recycleview
        cardrecycle = (RecyclerView) view.findViewById(R.id.bangumi_cardrecycle);
        cardrecycle.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        bangumi_monthrecycle.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        bangumirecycleadapter = new Bangumirecycleadapter(getActivity());
        recyclerView.setAdapter(bangumirecycleadapter);

        bangumimothadapter = new Bangumimothadapter(getActivity());
        bangumi_monthrecycle.setAdapter(bangumimothadapter);

        bangcardadapter = new Bangcardadapter(getActivity());
        cardrecycle.setAdapter(bangcardadapter);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        HttpGetData.RequestGet(getActivity(), URLUtils.DRAMA_HEAD + URLUtils.DRAMA_DATA, "zxw", new VolleyInterface(getActivity(), VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                try {
                    String json = new String(result.getBytes("ISO-8859-1"), "UTF-8");
                    BangumiBean bangumiBean = GsonUtil.parseJsonToObj(json, new TypeToken<BangumiBean>() {
                    });
                    List<BangumiBean.ResultEntity.AdEntity.HeadEntity> head = bangumiBean.getResult().getAd().getHead();
                    List<String> strings = new ArrayList<String>();
                    for (int i = 0; i < head.size(); i++) {
                        String img = head.get(i).getImg();
                        strings.add(img);
                    }
                    //设置图片加载器
                    banner.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    banner.setImages(strings);
                    //banner设置方法全部调用完毕时最后调用
                    banner.start();
                    //第一个Recycleview的适配
                    List<BangumiBean.ResultEntity> resultEntities = new ArrayList<BangumiBean.ResultEntity>();
                    resultEntities.add(bangumiBean.getResult());
                    bangumirecycleadapter.initdata(resultEntities);

                    //第二个Recycleview的适配
                    List<BangumiBean.ResultEntity.PreviousEntity> previousEntities = new ArrayList<BangumiBean.ResultEntity.PreviousEntity>();
                    previousEntities.add(bangumiBean.getResult().getPrevious());
                    bangumimothadapter.initdata(previousEntities);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                initcard();
            }

            @Override
            public void onMyError(VolleyError error) {
            }
        });


    }

    private void initcard() {

        HttpGetData.RequestGet(getActivity(), URLUtils.DRAMA_HEAD + URLUtils.DRAMA_RECOMMEND, "zxw", new VolleyInterface(getActivity(), VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                try {
                    String json = new String(result.getBytes("ISO-8859-1"), "UTF-8");

                    BangcardBean bangumiBean1 = GsonUtil.parseJsonToObj(json, new TypeToken<BangcardBean>() {
                    });
                    List<BangcardBean.ResultEntity> result1 = bangumiBean1.getResult();
                    bangcardadapter.initdata(result1);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
    }
}
